//
//  PKMusicPlayer.m
//  MusicDemo
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 CJ. All rights reserved.
//

#import "PKMusicPlayer.h"

#import <AVFoundation/AVFoundation.h>

@interface PKMusicPlayer ()

@property (nonatomic, strong) AVPlayer *player; //音乐播放器



@end


@implementation PKMusicPlayer

//单例
+(instancetype)sharedPlayer
{
    static PKMusicPlayer *player = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[PKMusicPlayer alloc] init];
    });
    return player;
}

//初始化
- (instancetype)init
{
    self = [super init];
    if (self) {
        _player = [[AVPlayer alloc]init];
        //默认下标为0
        _index = 0;
    }
    return self;
}



- (void)setMusicArray:(NSArray *)musicArray
{
    //相当于个音乐播放器数组初值的时候,同时会设置播放器的默认播放的歌曲是第一首
    _musicArray = musicArray;
   // AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:_musicArray[_index]]];
   // _url = _musicArray[_index];
   // [_player replaceCurrentItemWithPlayerItem:item];
  
}


//上一首
- (void)beforeMusic
{
    //下标减1 前一首
    _index--;
    
    //如果是第一首 跳转到最后一首
    if (_index == -1) {
        _index = _musicArray.count - 1;
    }
    //随机播放
    if (_playType == Random) {
        _index = arc4random()% _musicArray.count;
    }
    //播放下一曲
    [self changeMusicWithIndex:_index];
    
}


//下一首
- (void)nextMusic
{
    _index++;
    if (_index == _musicArray.count) {
        _index = 0;
    }
    //随机播放
    if (_playType == Random) {
        _index = arc4random()% _musicArray.count;
    }
    
    //播放下一曲
    [self changeMusicWithIndex:_index];
}

//播放
- (void)play
{
    [_player play];
}

//暂停
- (void)pause
{
    [_player pause];
}

//停止
- (void)stop
{
    [_player pause];
    [self seekToTime:0];  //播放进度置零
}

//给一个时间进行跳转
- (void)seekToTime:(NSInteger)newTime
{
    //value/timescale = seconds
    //value = timescale * seconds
    
    //value 总帧数
    //timescale 每秒帧数
    
    CMTime currentTime = _player.currentTime;
    currentTime.value = newTime * currentTime.timescale;
    [_player seekToTime:currentTime];
    
}

//当前时间
- (NSInteger)currentTime
{
    if (_player.currentTime.timescale == 0) {
        return 0;
    }
    return  _player.currentTime.value / _player.currentTime.timescale;
    
}

//总时间
- (NSInteger)finishTime
{
    CMTime time = _player.currentItem.duration;
    if (time.timescale == 0) {
        return 0;
    }
    return time.value / time.timescale;
}


//播放完毕后的操作
- (void)finishPlay
{
    if (_playType == single) {
        [self seekToTime:0];  //单曲循环
    } else {
        
        [self nextMusic];   // 下一曲
    }

}




//播放状态
- (BOOL)isPlaying
{
    //判断播放状态
    if (_player.rate == 1.0) {
        return YES;
    } else {
        return NO;
    }
}




//直接播放某一下标处歌曲
- (void)changeMusicWithIndex:(NSInteger)index
{
    //将要播放处下标赋值当前下标
    _index = index;
    
    _url = _musicArray[_index];
    NSURL *url = [NSURL URLWithString:_url];
    
    
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    [_player replaceCurrentItemWithPlayerItem:item];
    
    //播放
    [_player play];
 
}













@end
